package cc.ycws.core.config;

import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * Created by tyf on 2018/3/20
 */
@Component
public class MybatisPropertiesPostProseccer implements BeanPostProcessor, Ordered {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean.getClass().getName().equals(MybatisProperties.class.getName())) {
            MybatisProperties mybatisProperties = (MybatisProperties) bean;
            mybatisProperties.setMapperLocations(new String[]{"classpath:mappers/*.xml"});
            mybatisProperties.setTypeAliasesPackage("cc.ycws.core.domain");
            mybatisProperties.setConfigLocation("classpath:mybatis/mybatis.xml");
            return mybatisProperties;
        }
        return bean;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
